<template>
  <view>
    <button open-type="getUserInfo" @getuserinfo="wxLogin">点我登录</button>
  </view>
</template>

<script>
export default {
  methods: {
    wxLogin (userInfoRes) {
      // 用户信息
      console.log(userInfoRes.detail)
      let { encryptedData, iv, rawData, signature } = userInfoRes.mp.detail
      // 获取code
      wx.login({
        success: async loginRes => {
          console.log(loginRes)
          // 发请求
          let data = await this.$request({
            url: '/api/public/v1/users/wxlogin',
            method: 'POST',
            data: {
              code: loginRes.code,
              encryptedData,
              iv,
              rawData,
              signature
            }
          })

          console.log(data)
          wx.setStorageSync('token', data.token)
          // 存用户信息
          wx.setStorageSync('userInfo', userInfoRes.mp.detail.userInfo)

          // 返回上一页
          wx.navigateBack({})
        }
      })
    }
  }
}
</script>

<style></style>
